package gov.pbc.nn.kjgl.maintenance.view;

import gov.pbc.nn.core.bdf.IUserSelect;
import gov.pbc.nn.core.bdf.entity.Dept;
import gov.pbc.nn.core.bdf.service.UserService.DefaultUserRowMapper;
import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.maintenance.entity.SysClerk;
import gov.pbc.nn.kjgl.maintenance.entity.SysEntity;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/**
 * 综合体，操作数据加业务逻辑
 * 
 * @author jack.zhao
 * 
 */
@Component
public class SysClerkPR implements IUserSelect {
	@Resource(name = CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao hibernateDao;

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	@Resource(name = PbcnnCoreJdbcDao.BEAN_ID)
	private PbcnnCoreJdbcDao jdbcDao;

	@DataProvider
	public void loadSys(Page<SysEntity> page, Criteria criteria) {
		DetachedCriteria dc = this.hibernateDao.buildDetachedCriteria(criteria, SysEntity.class);
		dc.add(Restrictions.eq("invalid", false));
		dc.add(Restrictions.eq("auditState", 2));
		Dept dept = (Dept) ContextHolder.getLoginUser().getDepts().get(0);
		Dept zzDept = (Dept) SecurityUtils.getZZByDept(dept);
		if (!Dept.NANNING.equals(zzDept.getId())) {
		dc.add(Restrictions.or(Restrictions.eq("deptId", dept.getId()), Restrictions.eq("operationDeptId", dept.getId())));
		}
//		dc.add(Restrictions.eq("deptId", ContextHolder.getLoginUser().getDepts().get(0).getId()));
		this.hibernateDao.pagingQuery(page, dc);
	}

	@DataProvider
	public void loadSysClerk(Page<SysClerk> page, Criteria criteria, String id) {
		if (StringUtils.isNotEmpty(id)) {
			DetachedCriteria dc = this.hibernateDao.buildDetachedCriteria(criteria, SysClerk.class);
			dc.add(Restrictions.eq("invalid", false));
//			dc.add(Restrictions.eq("deptId", ContextHolder.getLoginUser().getDepts().get(0).getId()));
			dc.add(Restrictions.eq("sysId", id));
			this.hibernateDao.pagingQuery(page, dc);
		}
	}

	@DataResolver
	@Transactional
	public void saveSysClerk(SysClerk clerk, Map<String, Object> parameter) {
		this.saveAll(clerk);
	}

	public void saveAll(SysClerk clerk) {
		EntityState state = EntityUtils.getState(clerk);
		if (state.equals(EntityState.NEW)) {
			this.hibernateDao.save(clerk);
		} else if (state.equals(EntityState.DELETED)) {
			this.hibernateDao.delete(clerk);
		} else if (state.equals(EntityState.MODIFIED)) {
			this.hibernateDao.update(clerk);
		}
	}

	@Expose
	@Transactional
	public void deleteSysClerk(String id) {
		this.hibernateDao.delete(id, SysClerk.class);
	}

	@Override
	public void loadUsers(Page<IUser> page, Criteria criteria, Map<String, Object> params) throws Exception {
		String deptId = (String) params.get("deptId");
		String companyId = ContextHolder.getLoginUser().getCompanyId();

		String sql = "SELECT\n" + "	T.*\n" + "FROM\n" + "	(\n" + "		SELECT\n"
				+ "			U.*, D.ID_ AS DEPT_ID_,D.NAME_ as DEPT_NAME_\n" + "		FROM\n" + "			t_user U\n"
				+ "		LEFT JOIN bdf2_user_dept UD ON U.USERNAME_ = UD.USERNAME_\n"
				+ "		LEFT JOIN t_dept D ON UD.DEPT_ID_ = D.ID_ order by D.ID_,U.USERNAME_\n" + "	) T\n"
				+ "WHERE\n" + "	T.DEPT_ID_ like :deptId  and T.COMPANY_ID_=:companyId";
		ParseResult result = jdbcDao.parseCriteria(criteria, true, "T");
		if (result != null) {
			StringBuffer sb = result.getAssemblySql();
			Map<String, Object> valueMap = result.getValueMap();
			valueMap.put("deptId", deptId + "%");
			valueMap.put("companyId", companyId);
			sql += " and " + sb.toString();
			jdbcDao.pagingQuery(page, sql, new DefaultUserRowMapper(), valueMap);
		} else {
			Map<String, Object> parameter = new HashMap<String, Object>();
			parameter.put("deptId", deptId + "%");
			parameter.put("companyId", companyId);
			jdbcDao.pagingQuery(page, sql, new DefaultUserRowMapper(), parameter);
		}
	}
}
